package demo518set;

import demo516.Emp;

import java.util.*;

public class TestSet {
    private Set <Emp> empSet = new HashSet<>();

    //1添加员工
    public void addEmp(Emp emp) {
        empSet.add(emp);
    }

    //2查询所有员工
    public void findAllEmp() {
        for (Emp emp : empSet) {
            System.out.println(emp);
        }
    }

    //3根据姓名查询员工
    public Emp findEmpName(String empName) {
        for (Emp emp : empSet) {
            if (empName.equals(emp.getName())) {
                return emp;
            }
        }
        return null;
    }

    //4根据编号删除员工
    public void deleteEmp(String empNumber) {
        empSet.removeIf(emp -> emp.getNumber().equals(empNumber));
    }

    //5根据编号查询员工
    public Emp findEmpNumber(String empNumber) {
        for (Emp emp : empSet) {
            if (empNumber.equals(emp.getNumber())) {
                return emp;
            }
        }
        return null;
    }

    //6 根据编号改年龄
    public boolean updateEmp(Emp updatedEmp) {
        Emp existingEmp = findEmpNumber(updatedEmp.getNumber());
        if (existingEmp != null) {
            empSet.remove(existingEmp);
            empSet.add(updatedEmp);
            return true;
        }
        return false;
    }

}